﻿using System;
using System.Collections.Generic;

namespace Dsl.Ped.Common.Helpers
{
    /// <summary>
    /// Helper methods for the C# language
    /// </summary>
    public static class LanguageHelper
    {
        /// <summary>
        /// Get the file-extension of a C# source code
        /// </summary>
        /// <returns>File-extension of a C# source code</returns>
        public static string GetSourceCodeExtension()
        {
            return ".cs";
        }

        /// <summary>
        /// Get the name of the class based on the file. The name equals the file name without an extension.
        /// </summary>
        /// <param name="inputFilePath">Input file which the class is derivated out of</param>
        /// <returns>Class name</returns>
        public static string GetClassNameFromFile(string inputFilePath)
        {
            return string.IsNullOrEmpty(inputFilePath) ? 
                string.Empty : System.IO.Path.GetFileNameWithoutExtension(inputFilePath);
        }

        /// <summary>
        /// Get the namespace which will be used for a class generated out of the file on input
        /// </summary>
        /// <param name="defaultNamespace">Default namespace of the class</param>
        /// <param name="relativeFilePath">Relative path of the file</param>
        /// <returns>Namespace of the class generated out of the file specified</returns>
        public static string GetNamespaceFromFile(string defaultNamespace, string relativeFilePath)
        {
            if (string.IsNullOrEmpty(defaultNamespace))
                throw new ArgumentException("default namespace can't be empty", defaultNamespace);

            if (string.IsNullOrEmpty(relativeFilePath)) return defaultNamespace;

            var splittedPath = relativeFilePath.Split(new char[]{'\\'});
            if (splittedPath.Length == 0) return defaultNamespace;

            var splittedPathList = new List<string>(splittedPath);
            splittedPathList.RemoveAt(splittedPathList.Count-1);
            var relativeNamespace = string.Join(".", splittedPathList);
          
            return string.IsNullOrEmpty(relativeNamespace)
                       ? defaultNamespace
                       : string.Format("{0}.{1}", defaultNamespace, relativeNamespace);
        }
    }
}
